layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        optionGroup = 0;

    form.on("submit(editCompany)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        const { companyId: id, company } = data.field;
        console.log(data.field)
        var companyInfo = {
            id,
            company
        }

        if (data.field.addOrUpdate == 0) {//添加
            $.ajax({
                url: baseUrl + "company/addCompany.php",
                data: companyInfo,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '添加公司信息成功！请到用户管理为该公司创建账号！')
                    } else {
                        message(index, '添加公司信息失败！')
                    }
                }
            })
        } else {//修改
            $.ajax({
                url: baseUrl + "company/updateCompany.php",
                data: companyInfo,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '修改公司信息成功！')
                    } else {
                        message(index, '修改公司信息失败！')
                    }
                }
            })
        }
        return false;
    })

    function message(index, msg) {
        setTimeout(function () {
            top.layer.close(index);
            top.layer.msg(msg);
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        }, 1000);
    }
})